<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Project extends CI_Controller {

    public function __construct() {
        parent::__construct();
         $this->load->model('project_model', '', TRUE);
    }
  function index() {
        
         $history = $this->project_model->getHistory();
        $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/history_view', array("history" => $history))
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }
    
    function add() {
       
        $plug = array(
            'custom-plugins/fileinput',
            'plugins/validate/jquery.validate-min',
            'custom-plugins/wizard/wizard.min',
            'custom-plugins/wizard/jquery.form.min',
            
             // upload
            'plugins/plupload/plupload',
            'plugins/plupload/plupload.flash',
            'plugins/plupload/plupload.html4',
            'plugins/plupload/plupload.html5',
            'plugins/plupload/jquery.plupload.queue/jquery.plupload.queue',
            'plugins/elfinder/js/elfinder.min',
            'js/demo/demo.files'
        );
        $this->template->append_metadata(script($plug));
        $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/create_project')
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }

    function upload_resources() {
        $config['upload_path'] = RESOURCES_PATH;
        $config['allowed_types'] = 'zip';
        $config['overwrite'] = true;
        $data = array();

        $this->form_validation->set_rules('projname', 'nom du projet', 'required|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            $data['success'] = '0';
            $data['errors'] = form_error('projname');
            echo json_encode($data);
            exit();
        }

        $this->load->library('upload', $config);
        $file_name = "resfile";

        if (!$this->upload->do_upload($file_name)) {
            $data['success'] = "0";
            $data['error'] = $this->upload->display_errors();
            echo json_encode($data);
            return;
        } else {
            $data['project_name'] = $this->input->post('projname');
            $data['file_name'] = $this->upload->data()['file_name'];
            $ret = $this->create_project($data);
            echo json_encode($ret);
        }
    }

    private function create_project($data) {
        $this->load->helper('file');
        $path = PROJECTS_PATH . $data['project_name'];
        $info['success'] = "0";
        if (is_dir($path)) {
            delTree($path);
        }

        if (!create_dir($path)) {
            $info['error'] = "CAN_NOT_CREATE_DIR";
        } 
        else 
        {
            $this->load->helper('directory');
            $source = RESOURCES_PATH . $data['file_name'];
            if (extract_zip_to($source, $path)) {
                $info['success'] = '1';
                $svg_files = directory_map($path, 1);
                $file_tree = create_tree($svg_files);
                $info['list'] = '<ul>';
                $list_items = get_list_items($file_tree);

                foreach ($list_items as $l)
                    $info['list'] .= "<li><a>" . utf8_encode($l) . "</a></li>";
                $info['list'] .= "\n<ul>";
            } else 
            {
                $info['error'] = "CAN_NOT_EXTRACT_ARCHIVE";
                rmdir($path);
            }
        }

        return $info;
    }

    private function step_two($data = null) {

        if ($data == null || !is_array($data))
            return;
        $this->load->helper('file');
        $path = PROJECTS_PATH . $data['project_name'];
        if (is_dir($path)) {

            $info['error'] = "Le projet :<u>" . $data['project_name'] . " existe deja !";
        } else {

            if (!create_dir($path)) {
                $info['error'] = "Des erreur sont survenu pandont la creation du repertoire :$path";
            } else {
                $this->load->helper('directory');
                $source = RESOURCES_PATH . $data['resource']['file_name'];
                if (extract_zip_to($source, $path)) {
                    $info['svg_files'] = directory_map($path, 1);
                    $info['file_tree'] = create_tree($info['svg_files']);
                    $info['list'] = create_navigation_menu($info['file_tree']);
                } else {
                    $info['error'] = "le fichier" . $data['resource']['file_name'] . " n'a pas pus être extrait ";
                }
            }
        }

        return $info;
    }

    function attachement() {


        $meta = array(
            'custom-plugins/fileinput',
            'plugins/validate/jquery.validate-min',
            'custom-plugins/wizard/wizard.min',
            'custom-plugins/wizard/jquery.form.min',
            'js/demo/demo.wizard',
            // upload
            'plugins/plupload/plupload',
            'plugins/plupload/plupload.flash',
            'plugins/plupload/plupload.html4',
            'plugins/plupload/plupload.html5',
            'plugins/plupload/jquery.plupload.queue/jquery.plupload.queue',
            'plugins/elfinder/js/elfinder.min',
            'js/demo/demo.files'
        );

        foreach ($meta as $l)
            $this->template->append_metadata(js($l));

        $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/attachement')
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }

}
